/*
 *
 * 名称：六十四卦
 * 功能：易经的六十四卦功能
 * 作者：清河张
 * 时间：2025.05.30
 *
 */
#include "gua64.h"
FUGUA_T::XIANG::XIANG(unsigned int v)
{
    _v = v;
}

FUGUA_T::XIANG::XIANG(const DANGUA_T::XIANG& sg, const DANGUA_T::XIANG& xg):
    _v(0)
{
    if(sg.invalid()||xg.invalid())
    {
        _a = 1;
        _b = 1;
    }
    else
    {
        _6 = sg._3;
        _5 = sg._2;
        _4 = sg._1;
        _3 = xg._3;
        _2 = xg._2;
        _1 = xg._1;
    }

}

FUGUA_T::XIANG FUGUA_T::XIANG::zheng()
{
    return XIANG(_v);
}

FUGUA_T::XIANG FUGUA_T::XIANG::hu()
{
    XIANG h;
    h._6 = _5;
    h._5 = _4;
    h._4 = _3;
    h._3 = _4;
    h._2 = _3;
    h._1 = _2;
    return h;
}

FUGUA_T::XIANG FUGUA_T::XIANG::cuo()
{
    XIANG c;
    c._6 = !(_6 & 1);
    c._5 = !(_5 & 1);
    c._4 = !(_4 & 1);
    c._3 = !(_3 & 1);
    c._2 = !(_2 & 1);
    c._1 = !(_1 & 1);
    return c;
}

FUGUA_T::XIANG FUGUA_T::XIANG::zong()
{
    XIANG z;
    z._6 = _1;
    z._5 = _2;
    z._4 = _3;
    z._3 = _4;
    z._2 = _5;
    z._1 = _6;
    return z;
}

FUGUA_T::XIANG FUGUA_T::XIANG::bian()
{

    XIANG b;
    b._6 = ((_6 & 1) == (_6 >>1)) + _6;
    b._5 = ((_5 & 1) == (_5 >>1)) + _5;
    b._4 = ((_4 & 1) == (_4 >>1)) + _4;
    b._3 = ((_3 & 1) == (_3 >>1)) + _3;
    b._2 = ((_2 & 1) == (_2 >>1)) + _2;
    b._1 = ((_1 & 1) == (_1 >>1)) + _1;
    return b;
}

bool FUGUA_T::XIANG::invalid() const
{
    return _a || _b;
}


template <> FUGUA_T GUA64PARENT::_objs[65] =
{
 {  0,192,  2, "空",},
 {  1, 63, 24, "乾",},
 {  2,  0,  7, "坤",},
 {  3, 17, 19, "屯",},
 {  4, 34, 15, "蒙",},
 {  5, 23, 36, "需",},
 {  6, 58, 46, "讼",},
 {  7,  2, 11, "师",},
 {  8, 16, 16, "比",},
 {  9, 55, 51, "小畜",},
 { 10, 59, 40, "履",},
 { 11,  7, 54, "泰",},
 { 12, 56, 62, "否",},
 { 13, 61, 55, "同人",},
 { 14, 47, 32, "大有",},
 { 15,  4, 34, "谦",},
 { 16,  8,  8, "豫",},
 { 17, 25,  3, "随",},
 { 18, 38, 29, "蛊",},
 { 19,  3, 60, "临",},
 { 20, 48, 39, "观",},
 { 21, 41, 63, "噬嗑",},
 { 22, 37, 48, "贲",},
 { 23, 32,  5, "剥",},
 { 24,  1, 45, "复",},
 { 25, 57, 17, "无妄",},
 { 26, 39, 47, "大畜",},
 { 27, 33, 58, "颐",},
 { 28, 30, 31, "大过",},
 { 29, 18, 49, "坎",},
 { 30, 45, 28, "离",},
 { 31, 28, 43, "咸",},
 { 32, 14, 23, "恒",},
 { 33, 60, 27, "遁",},
 { 34, 15,  4, "大壮",},
 { 35, 40, 41, "晋",},
 { 36,  5, 52, "明夷",},
 { 37, 53, 22, "家人",},
 { 38, 43, 18, "睽",},
 { 39, 20, 26, "蹇",},
 { 40, 10, 35, "解",},
 { 41, 35, 21, "损",},
 { 42, 49, 64, "益",},
 { 43, 31, 38, "夬",},
 { 44, 62, 56, "姤",},
 { 45, 24, 30, "萃",},
 { 46,  6, 50, "升",},
 { 47, 26, 14, "困",},
 { 48, 22, 20, "井",},
 { 49, 29, 42, "革",},
 { 50, 46, 59, "鼎",},
 { 51,  9, 61, "震",},
 { 52, 36, 53, "艮",},
 { 53, 52, 37, "渐",},
 { 54, 11, 57, "归妹",},
 { 55, 13,  9, "丰",},
 { 56, 44, 12, "旅",},
 { 57, 54, 25, "巽",},
 { 58, 27,  6, "兑",},
 { 59, 50, 10, "涣",},
 { 60, 19, 33, "节",},
 { 61, 51, 13, "中孚",},
 { 62, 12, 44, "小过",},
 { 63, 21,  1, "既济",},
 { 64, 42,  0, "未济",},
};


FUGUA_T::FUGUA_T():
    CT(-1, "空"),
    _xiang(0)
{
}


FUGUA_T::FUGUA_T(int id, unsigned xiang, int xu, const char *name):
    CT(id, name), _xiang(xiang), _xu(xu)
{

}

FUGUA_T::XIANG FUGUA_T::xiang() const
{
    return _xiang;
}

int FUGUA_T::xu() const
{
    return _xu;
}

GUA64::GUA64():
    GUA64PARENT(-1)
{

}

GUA64::GUA64(int id):
    GUA64PARENT(id)
{

}

GUA64::GUA64(const FUGUA_T &GUA64)
    : GUA64PARENT (GUA64)
{

}

GUA64::GUA64(const GUA64PARENT &parent):
    GUA64PARENT (parent)
{

}

GUA64 &GUA64::operator = (const GUA64PARENT &GUA64)
{
    GUA64PARENT::operator = (GUA64);
    return *this;
}


GUA64 &GUA64::operator = (const FUGUA_T &GUA64)
{
    GUA64PARENT::operator = (GUA64);
    return *this;
}

GUA64 &GUA64::operator =(int id)
{
    GUA64PARENT::operator =(id);
    return *this;
}


GUA64 GUA64::fu(const GUA8 &s, const GUA8 &x)
{
    XIANG gx( s.xiang(), x.xiang() );
    return xiang(gx);
}

GUA64 GUA64::xiang(const XIANG& x)
{
    if(x.invalid()) return -1;
    GUA64 gua = index((int)x._v);
    return index(gua.xu());
}
